clear
do "usa_00066.do"


merge m:1 stateicp countyicp using "icpfips.dta"

save "ind_1940_ctyfip.dta", replace

keep if hasnormalorasylum==1

bysort cty_fips: egen totemp = total(empstat==1)

gen ag = ind1950>=105 & ind1950<=126
replace ag = . if ind1950==.

gen mining = ind1950>=206 & ind1950<=239
replace mining = . if ind1950==.

gen constr = ind1950==246
replace constr = . if ind1950==.

gen mfg = ind1950>=306 & ind1950<=499
replace mfg = . if ind1950==.

gen transp = ind1950>=506 & ind1950<=598
replace transp = . if ind1950==.

gen fire = ind1950>=716 & ind1950<=756
replace fire = . if ind1950==.

gen srv = ind1950>=806 & ind1950<=899
replace srv = . if ind1950==.

gen pub = ind1950>=906 & ind1950<=936
replace pub = . if ind1950==.

gen trade = ind1950>=626 & ind1950<=699
replace trade = . if ind1950==.

gen busrepairsrv = ind1950>=806 & ind1950<=817
replace busrepairsrv = . if ind1950==.

gen perssrv = ind1950>=826 & ind1950<=849
replace perssrv = . if ind1950==.

gen entsrv = ind1950>=856 & ind1950<=859
replace entsrv = . if ind1950==.

gen profsrv = ind1950>=868 & ind1950<=899
replace profsrv = . if ind1950==.

local vars ag mining constr mfg transp fire srv pub trade busrepairsrv perssrv entsrv profsrv
foreach x of local vars{
bysort cty_fips empstat: egen tot`x' = total(`x') if empstat==1
gen frac`x'emp = tot`x'/totemp
}

egen tagctyemp = tag(cty_fips empstat)

local vars ag mining constr mfg transp trade fire busrepairsrv perssrv entsrv profsrv  pub  
foreach x of local vars{
reghdfe frac`x'emp hasnormalschool if tagctyemp==1 & empstat==1, absorb(stateicp) cluster(stateicp)
outreg2 using ind1940.xls, append excel dec(3)

#delimit ;
tabout hasnormalschool if hasnormalorasylum==1 & tagctyemp==1 & empstat==1 using sumstats_1940.xls,
append c(mean frac`x'emp sd frac`x'emp) npos(row) f(2)  layout(row) sum show(none); 
#delimit cr

su frac`x'emp if hasnormalorasylum==1 & tagctyemp==1 & empstat==1 & hasnormalschool==1
su frac`x'emp if hasnormalorasylum==1 & tagctyemp==1 & empstat==1 & hasnormalschool==0
}

save "ind_1940_ctyfip_NA.dta", replace

*Principal component for Appendix Table A36

keep if tagctyemp==1 & empstat==1 & hasnormalorasylum==1
keep cty_fips hasnormalschool hasnormalorasylum stateicp fracag fracmining fracconstr fracmfg fractransp fractrade fracfire fracbusrepairsrv fracperssrv fracentsrv fracprofsrv  fracpub  

save "1940_ind_small.dta", replace

clear
use "nhgis_1940.dta"

keep if hasnormalorasylum==1
#delimit ;
keep cty_fips fracprof 
fracsemiprof fracfarmer fracmgr fracclerk fraccrafts fracoper fracdomestic 
fracsrv fraclaborer;

#delimit cr

merge 1:1 cty_fips using "1940_ind_small.dta"
rename _merge mergepcaindocc

#delimit ;
pca fracprof fracsemiprof fracfarmer fracmgr fracclerk fraccrafts fracoper fracdomestic fracsrv fraclaborer  
fracag fracmining fracconstr fracmfg fractransp fractrade fracfire fracbusrepairsrv 
fracperssrv fracprofsrv  fracpub;  

predict pc1 pc2 pc3 pc4 pc5 pc6 pc7 pc8 pc9 pc10 pc11 pc12 pc13 pc14 pc15 pc16 pc17 pc18 pc19 pc20 pc21, score;

#delimit cr
reghdfe pc1 hasnormalschool, absorb(stateicp) cluster(stateicp)
outreg2 using ind1940.xls, append excel dec(3)

su pc1 if hasnormalorasylum==1 & hasnormalschool==1
su pc1 if hasnormalorasylum==1 & hasnormalschool==0
